package com.itlong.cloud.controller.sharedELevator;

import com.itlong.cloud.POJO.DTO.property.ConsumptionRecordDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.ConsumptionRecordVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.equip.property.IPropertyConsumptionServiceClient;
import com.itlong.cloud.project.app.IAppProjectServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.app.IAppHomeServiceClient;
import com.itlong.cloud.user.app.IAppMineServiceClient;
import com.itlong.cloud.utils.export.ExportUtil;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      物业消费管理控制器
 * </desc>
 *
 * @createDate2020/7/15
 **/
@RestController
@RequestMapping("/sharedElevator/consumption")
public class PropertyConsumptionController {

    @Autowired
    private IPropertyConsumptionServiceClient iPropertyConsumptionServiceClient;

    @Autowired
    private IAppProjectServiceClient iAppProjectServiceClient;

    @Value("${" + PlatformConstants.FILE_FTP_HOST + "}")
    public String FILE_FTP_HOST;

    @Value("${" + PlatformConstants.FILE_FTP_PORT + "}")
    public String FILE_FTP_PORT;

    @Value("${" + PlatformConstants.FILE_FTP_USERNAME + "}")
    public String FILE_FTP_USERNAME;

    @Value("${" + PlatformConstants.FILE_FTP_PASSWORD + "}")
    public String FILE_FTP_PASSWORD;

    @Value("${" + PlatformConstants.FILE_ACCESS_URL + "}")
    public String FILE_ACCESS_URL;

    /**
     * <desc>
     *      分页获取消费管理列表
     * </desc>
     *
     * @param consumptionRecordDTO 消费记录DTO
     * @return 消费记录分页列表
     * @Author Zhu.yj
     * @createDate  2020/7/16
     */
    @RequestMapping(value = "getPage", method = RequestMethod.POST)
    public Object getPage(ConsumptionRecordDTO consumptionRecordDTO, HttpServletRequest request){
        try{
            consumptionRecordDTO = HttpProtocolUtil.parseRequestParamToDTO(ConsumptionRecordDTO.class, request);
            if (consumptionRecordDTO == null || StringUtils.isBlank(consumptionRecordDTO.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<ConsumptionRecordVO> page = iPropertyConsumptionServiceClient.getPage(consumptionRecordDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), page);
        }catch (Exception e){
            throw new DataAccessException("【物业_消费管理_分页获取消费记录】分页获取消费记录失败", e);
        }
    }

    /**
     * <desc>
     *      导出消费记录
     * </desc>
     *
     * @return
     * @Author Zhu.yj
     * @createDate  2020/7/20
     */
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public Object export(ConsumptionRecordDTO consumptionRecordDTO, HttpServletRequest request){
        try{
            consumptionRecordDTO = HttpProtocolUtil.parseRequestParamToDTO(ConsumptionRecordDTO.class, request);
            if (consumptionRecordDTO == null || StringUtils.isBlank(consumptionRecordDTO.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<ConsumptionRecordVO> page = iPropertyConsumptionServiceClient.getPage(consumptionRecordDTO);
            List<ConsumptionRecordVO> list = page.getRows();
            if (list == null || list.isEmpty()){
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_EMPTY_ERR.getErrorCode());
            }
            for (ConsumptionRecordVO consumptionRecordVO : list){
                consumptionRecordVO.setPassType(consumptionRecordVO.getPassTypeString(consumptionRecordVO.getPassType()));
            }
            String exportItemNames = "序号,房间号,通行方式,身份,通行人员,通行时间,设备名称";
            String exportItems = "serialNum,roomId,passType,houseType,userName,createTime,deviceName";
            String exportFileUrl = ExportUtil.exportByItemsAndNames(list, exportItemNames, exportItems,
                    (short) 420, 16, consumptionRecordDTO.getProjectId(), FILE_FTP_HOST, FILE_FTP_USERNAME, FILE_FTP_PASSWORD, FILE_FTP_PORT, FILE_ACCESS_URL);
            if (StringUtils.isBlank(exportFileUrl)){
                return new MessageVO(PropertyErrorCodeEnum.PROJECT_EXPORT_FILE_SAVE_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object) exportFileUrl);
        }catch (Exception e){
            throw new DataAccessException("【物业_消费管理_export】导出消费记录失败", e);
        }
    }

    /**
     * <desc>
     *      获取人员类型（包括访客）
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2021/5/18
     */
    @PostMapping("getUserTypeList")
    public Object getUserTypeList(String projectId, HttpServletRequest request){
        try{
            projectId =request.getAttribute("projectId") == null ? null : request.getAttribute("projectId").toString();
            if (StringUtils.isBlank(projectId)){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            List<Map<String, Object>> userTypeList = iAppProjectServiceClient.getProjectUserRoleList(projectId);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), userTypeList);
        }catch (Exception e){
            throw new DataAccessException("【物业_消费记录_getUserTypeList】获取人员类型失败");
        }
    }
}
